<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/7/7 0007
 * Time: 16:38
 */

namespace Api\Controller;


use Think\Controller;

class WxQrcodeController extends Controller
{
    //生成微信关注二维码
    public function index()
    {
        $ac = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_API.appid').'&secret='.C('WX_API.secret'));
        $wxt = json_decode($ac,true);

        $data = array(
            "action_name"=> "QR_LIMIT_STR_SCENE",
            "action_info"=> [
                "scene"=> [
                    "scene_str"=> "xiangtws"
                ],
            ]
        );

        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?"."access_token=".$wxt['access_token'];
        $curl = curl_init($url);
        $header = array();
        $header[] = 'Content-Type: application/x-www-form-urlencoded';
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        // 不输出header头信息
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 伪装浏览器
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        // 保存到字符串而不是输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($curl, CURLOPT_POST, 1);
        // 请求数据
        curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($data));
        $response = curl_exec($curl);
        curl_close($curl);

        $ticket = json_decode($response,true);;
        redirect("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket['ticket']);
    }
}